/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-04 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLElement;

public abstract class AuxiliaryItem extends ReasoningItem implements IElement {

    private enum MySubclass {JustifiedProposition, Consider, Set, Reconsider, DefFunc, DefPred,}

    public static boolean isAuxiliaryItem(String className){
        boolean found = false;
        if (JustifiedProposition.isJustifiedProposition(className)) found = true;
        for (MySubclass mySubclass :MySubclass.values()){
            if (mySubclass.name().equals(className)){
                found = true;
                break;
            }
        }
        return found;
    }

    public static AuxiliaryItem newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        XMLElement xmlElement = xmlElementList.peek();
        if (!isAuxiliaryItem(xmlElement.getName())){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of AuxiliaryItem", xmlElementList);
        }
        if (JustifiedProposition.isJustifiedProposition(xmlElement.getName())){
        	return JustifiedProposition.newInstance(parentNode, xmlElementList);
        }
        MySubclass mySubclass = MySubclass.valueOf(xmlElement.getName());
        switch (mySubclass) {
        case Consider: return Consider.newInstance(parentNode, xmlElementList);
        case DefFunc: return DefFunc.newInstance(parentNode, xmlElementList);
        case DefPred: return DefPred.newInstance(parentNode, xmlElementList);
        case Reconsider: return Reconsider.newInstance(parentNode, xmlElementList);
        case Set: return Set.newInstance(parentNode, xmlElementList);
        default: throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a subclass of AuxiliaryItem", xmlElementList);
        }
    }

    public LinkedList<XMLElement> getXMLElementList() {
        if (this instanceof JustifiedProposition) {
            return ((JustifiedProposition) this).getXMLElementList();
        } else 
        if (this instanceof Consider) {
            return ((Consider) this).getXMLElementList();
        } else 
        if (this instanceof DefFunc) {
            return ((DefFunc) this).getXMLElementList();
        } else 
        if (this instanceof DefPred) {
            return ((DefPred) this).getXMLElementList();
        } else 
        if (this instanceof Reconsider) {
            return ((Reconsider) this).getXMLElementList();
        } else 
        if (this instanceof Set) {
            return ((Set) this).getXMLElementList();
        } else 
        return new LinkedList<XMLElement>();
    }
}
